<?php

defined('__VN_BASE') or define('__VN_BASE', dirname(__FILE__));

require_once 'vn.primatives.php';
require_once 'vn.database.php';
require_once 'vn.user.php';
require_once 'vn.error.php';

class VNBase extends VNDatabase {

	protected $uObj;
	
	protected $site_template;
	protected $site_location;
	protected $site_name;
	protected $site_slogan;
	protected $controller;

	public static $permissions = array(
				"Guest" 	=> "1",
				"Registered"	=> "2",
				"Support"	=> "3",
				"Maintenance"	=> "4",
				"Developer"	=> "5",
				"Staff"		=> "6",
				"Administrator"	=> "7");

	public function __Construct(bool $tpl = NULL){

		try{

			//construct the parent class
			parent::VNDatabase();
			
			/**
			 * This section will have to change
			 */
			$this->site_template = "lateralplains";
			$this->site_name = "CHAF Agricultural Monitoring";
			$this->site_slogan = "";
			$this->site_location = $this->VNcore['__VNSite'];

			$this->permissions = array(
				"Guest" 	=> "1",
				"Registered"	=> "2",
				"Support"	=> "3",
				"Maintenance"	=> "4",
				"Developer"	=> "5",
				"Staff"		=> "6",
				"Administrator"	=> "7");

			if($tpl){

				//initialize the template controller
				$tpl = & string($this->site_template);
				$this->controller = $this->loadTemplateController($tpl);
				$this->controller->loadTemplate();

				//print_r($this->controller);
				//print $tpl." ".$this->controller;

			}
			
			/*
			if($test){

				throw new VNException(__CLASS__,'Could Not Initiate VNBase',300);
			}
			*/

		}catch (EAPIException $e){

			print $e;
			die();

		}

	}
	public function __Destruct(){}

	private function loadTemplateController(string $tpl){

		if(file_exists('template/'.$tpl.'.php')){
			
			require_once 'template/'.$tpl.'.php';
			$str = $tpl->getValue();

		}else{

			return false;

		}

		return new $str($this); //$this = VNBase obj, temporarily required

	}

	public function loadPage(string &$page, array &$vars){

		return $this->controller->getContent($page, $vars);

	}

	
	public function getVNUser(){
		
		return $this->uObj;
		
	}
	
	public function getTemplate(){
		
		return $this->site_template;
		
	}

	public function getSiteName(){
		
		return $this->site_name;
		
	}

	public function getSiteSlogan(){
		
		return $this->site_slogan;
		
	}

	public function getSiteLocation(){
		
		return $this->site_location;
		
	}
/*	
	public function getCID($client){
		$q = "SELECT `id` FROM `clients` WHERE `name` = '".$client."'";
		$r = $this->query($q);
		$id = $this->fetchAssoc($r);
		return $id['id'];
	}
*/
};
?>
